package com.eleMall.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eleMall.entity.GoodsRecord;
import com.eleMall.entity.Result;
import com.eleMall.entity.ShoppingCart;
import com.eleMall.service.GoodsRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ximen
 * @since 2024-03-10
 */
@RestController
@RequestMapping("/eleMall/goods-record")
@Api(tags = "购物记录(GoodRecord)表控制层")
public class GoodsRecordController {
    /**
     * 服务对象
     */
    @Autowired
    private GoodsRecordService goodsRecordService;

    /**
     * 分页查询
     *
     * @param userId 使用者id
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping
    public Result<Page> page(int page, int pageSize,long userId){
        //分页构造器
        Page<GoodsRecord> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<GoodsRecord> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户id进行查询
        queryWrapper.eq(GoodsRecord::getUserId,userId);
        //添加排序条件，根据sort进行排序
        queryWrapper.orderByAsc(GoodsRecord::getId);

        //分页查询
        goodsRecordService.page(pageInfo,queryWrapper);
        return Result.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("{id}")
    public Result queryById(@ApiParam(value = "id 主键") @PathVariable("id") Long id) {
        return Result.success(this.goodsRecordService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param goodsRecord 实体
     * @return 新增结果
     */
    @ApiOperation("新增数据")
    @PostMapping
    public Result add(@ApiParam(value = "goodsRecord 实体") @RequestBody GoodsRecord goodsRecord) {
        goodsRecordService.save(goodsRecord);
        return Result.success();
    }

    /**
     * 编辑数据
     *
     * @param goodsRecord 实体
     * @return 编辑结果
     */
    @ApiOperation("编辑数据")
    @PutMapping
    public Result edit(@ApiParam(value = "goodsRecord 实体") @RequestBody GoodsRecord goodsRecord) {
        goodsRecordService.updateById(goodsRecord);
        return Result.success();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("删除数据")
    @DeleteMapping
    public Result deleteById(@ApiParam(value = "id 主键") Long id) {
        goodsRecordService.removeById(id);
        return Result.success();
    }

}
